package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.dao.AdminMapper;
import com.itheima.dao.RoleMapper;
import com.itheima.pojo.Admin;
import com.itheima.pojo.CategoryMenu;
import com.itheima.pojo.Role;
import com.itheima.service.AdminService;
import com.itheima.service.RoleService;
import com.itheima.utils.*;
import com.itheima.vo.AdminVO;
import com.itheima.vo.AuthVO;
import com.itheima.vo.CategoryMenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/*
* 管理员相关接口
* */
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员相关接口")
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private RedisTemplate redisTemplate;


    /*
     * 注册管理员
     * */
    @PostMapping("/add")
    @ApiOperation("注册管理员")
    public Result add(@RequestBody AuthVO authVO) {
        adminService.add(authVO);
        return new Result("success",MessageConstant.ADMIN_ADD_SUCCESS);
    }

    /*
     * 分配用户角色信息列表
     * */
    @PostMapping("/assign")
    @ApiOperation("分配用户角色信息列表")
    public String assign(@RequestParam String adminUid) {
        return adminService.assign(adminUid);
    }

    /*
     * 删除部分管理员信息
     * */
    @PostMapping("/delete")
    @ApiOperation("删除部分管理员信息")
    @ResponseBody
    public String delete(@RequestBody String adminUids) {
        if (adminUids == null){
            return ResultUtils.result("error", MessageConstant.ADMIN_DELETE_FAIL);
        }
        return adminService.delete(adminUids);
    }

    /*
     * 更新管理员基本信息
     * */
    @PostMapping("/edit")
    @ApiOperation("更新管理员基本信息")
    public String edit(@RequestBody Admin admin) {
        if (admin.getUid()==null||"".equals(admin.getUid())){
            return ResultUtils.result(500,MessageConstant.ADD_UID);
        }
        return adminService.edit(admin);
    }

    /*
     * 强退用户
     * */
    @PostMapping("/forceLogout")
    @ApiOperation("强退用户")
    public String forceLogout(@RequestBody String tokenList) {
        adminService.forceLogout(tokenList);
        return ResultUtils.result(200,MessageConstant.ADMIN_TOKEN);
    }

    /*
     * 获取管理员列表
     * */
    @PostMapping("/getList")
    public String getList(@RequestBody(required = false) AdminVO adminVO) {
        Page<Admin> logList = adminService.getList(adminVO);
        Map<String, Object> map = new HashMap();
        map.put("records",logList.getResult());
        map.put("total",logList.getTotal());
        map.put("size",logList.getPageSize());
        map.put("current",logList.getPageNum());
        map.put("orders","");
        map.put("optimizeCountSql",true);
        map.put("isSearchCount",true);
        return ResultUtil.result("success", map);
    }

    /**
     * 获取在线管理员列表
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getOnlineAdminList")
    @ApiOperation("获取在线管理员列表")
    public String getOnlineAdminList(@RequestParam Long currentPage,@RequestParam Long pageSize){
        //判断页数是否为空
        if (currentPage.intValue()<1 || null == currentPage){
            currentPage=1L;
        }
        //判断每页数据是否为空
        if (pageSize<1 || null == pageSize){
            pageSize=10L;
        }
        List<Admin> adminList = adminService.getOnlineAdminList(currentPage, pageSize);
        return ResultUtils.result("200",adminList);
    }

    /*
     * 重置用户密码
     * */
    @PostMapping("/restPwd")
    @ApiOperation("重置用户密码")
    public String restPwd(@RequestParam String uid) {
        return adminService.restPwd(uid);
    }

    /*
     * 更新管理员邮箱或手机号
     * */
    @PostMapping("/updateEmail")
    @ApiOperation("更新管理员邮箱或手机号")
    public String updateEmail(@RequestParam String uid, @RequestParam String newInfo, @RequestParam String code) {
            //更新管理员邮箱或手机号
            adminService.updateEmail(uid,newInfo);
            return ResultUtils.result(200,MessageConstant.ADMIN_EDIT_SUCCESS);
        }

}
